﻿using System;
using System.Security.Cryptography;

namespace EncryptHelpers
{
    public static class HashHelper
    {
        public static string ComputeHash(HashAlgorithm algorithm, string textToHash, bool removeDashes)
        {
            var inputBytes = EncryptHelper.GetBytesRegardlessOfEncoding(textToHash);
            var hashBytes = algorithm.ComputeHash(inputBytes);

            var hash = BitConverter.ToString(hashBytes);
            return removeDashes ? hash.Replace("-", string.Empty) : hash;
        }

        public static string ComputeHash(string hashName, string textToHash, bool removeDashes)
        {
            var algorithm = HashAlgorithm.Create(hashName);
            return ComputeHash(algorithm, textToHash, removeDashes);
        }

        /// <summary>
        /// Compute MD5 (Message Digest algorithm 5) hash.
        /// </summary>
        /// <param name="textToHash">Text to compute hash.</param>
        /// <returns></returns>
        public static string MD5CSP(string textToHash, bool removeDashes)
        {
            var md5 = new MD5CryptoServiceProvider();
            return ComputeHash(md5, textToHash, removeDashes);
        }

        /// <summary>
        /// Compute SHA1(Secure Hash Algorithm 1, output 160 bits).
        /// </summary>
        /// <param name="textToHash"></param>
        /// <returns></returns>
        public static string SHA1CSP(string textToHash, bool removeDashes)
        {
            var sha1 = new SHA1CryptoServiceProvider();
            return ComputeHash(sha1, textToHash, removeDashes);
        }

        /// <summary>
        /// Compute SHA256(Secure Hash Algorithm, output 256 bits).
        /// </summary>
        /// <param name="textToHash">Text to compute hash.</param>
        /// <returns></returns>
        public static string SHA256CSP(string textToHash, bool removeDashes)
        {
            var sha256 = new SHA256CryptoServiceProvider();
            return ComputeHash(sha256, textToHash, removeDashes);
        }

        /// <summary>
        /// Compute SHA384(Secure Hash Algorithm, output 384 bits).
        /// <para>NOTE: Only supported on 64 bits platforms.</para>
        /// </summary>
        /// <param name="textToHash">Text to compute hash.</param>
        /// <returns></returns>
        public static string SHA384CSP(string textToHash, bool removeDashes)
        {
            var sha384 = SHA384.Create();// new SHA384CryptoServiceProvider();
            return ComputeHash(sha384, textToHash, removeDashes);
        }

        /// <summary>
        /// Compute SHA512(Secure Hash Algorithm, output 512 bits).
        /// <para>NOTE: Only supported on 64 bits platforms.</para>
        /// </summary>
        /// <param name="textToHash">Text to compute hash.</param>
        /// <returns></returns>
        public static string SHA512CSP(string textToHash, bool removeDashes)
        {
            var sha512 = SHA512.Create();// new SHA512CryptoServiceProvider();
            return ComputeHash(sha512, textToHash, removeDashes);
        }
    }
}
